package com.dq.service.mall.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.request.BatchIntoCartRequest;
import com.dq.domain.goods.Product;
import com.dq.domain.goods.ShoppingBag;
import com.dq.domain.goods.ShoppingCart;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.AllRepository;
import com.dq.service.ShiroService;
import com.dq.service.mall.ShoppingBagService;
import com.dq.service.mall.ShoppingCartService;
import com.dq.service.mall.ShoppingService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Log
@Service
public class ShoppingServiceImpl extends AllRepository implements ShoppingService {

    @Autowired
    ShiroService shiroService;
    @Autowired
    protected ShoppingCartService shoppingCartService;
    @Autowired
    protected ShoppingBagService shoppingBagService;

    @Override
    public ShoppingBag intoMyCart(Product product, Long number) {
        SysUser sysUser = shiroService.getUser();
        ShoppingCart cart = shoppingCartService.getByUserAndOrg(sysUser);
        ShoppingBag bag = shoppingBagService.getByCartAndProduct(cart, product);
        if (null != number && number > 0) {
            bag.plus(number);
        } else {
            bag.plus(1L); // 加购1
        }
        return shoppingBagRepository.save(bag);
    }

    @Override
    public ShoppingBag setProductNumber(Product product, long number) {
        SysUser sysUser = shiroService.getUser();
        ShoppingCart cart = shoppingCartService.getByUserAndOrg(sysUser);
        ShoppingBag bag = shoppingBagService.getByCartAndProduct(cart, product);
        if (number < 0) number = 0;
        bag.setNumber(number);
        return shoppingBagRepository.save(bag);
    }

    @Override
    public ShoppingCart.Vo myCart() {
        SysUser sysUser = shiroService.getUser();
        ShoppingCart cart = shoppingCartService.getByUserAndOrg(sysUser);
        List<ShoppingBag> bags = shoppingBagRepository.findByCartAndNumberGreaterThanAndGoodsDeletedFalseOrderByCreateTimeDesc(cart, 0L); // 商品数大于0的购物袋
        return cart.toVo(bags);
    }

    @Override
    public ShoppingCart.Vo batchIntoMyCart(List<BatchIntoCartRequest> list)throws AdminException {
        SysUser sysUser = shiroService.getUser();
        ShoppingCart cart = shoppingCartService.getByUserAndOrg(sysUser);
        if(list.size() == 0){
            throw new AdminException("请先选择商品");
        }
        list.forEach(batchIntoCartRequest -> {
            Product product =dao.get(Product.class,batchIntoCartRequest.getProductId());
            ShoppingBag bag = shoppingBagService.getByCartAndProduct(cart, product);
            if (null != batchIntoCartRequest.getNum() && batchIntoCartRequest.getNum() > 0) {
                bag.plus(batchIntoCartRequest.getNum());
            } else {
                bag.plus(1L); // 加购1
            }
            shoppingBagRepository.save(bag);
        });
        return myCart();
    }
}
